/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codemanager.database.dao.impl;

import codemanager.database.dao.ProjectDAO;
import codemanager.database.pojo.Project;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author jiang tao
 */
public class ProjectDAOImpl implements ProjectDAO{
    
    private SessionFactory sessionFactory;
    private Session session;
    
    public ProjectDAOImpl() {
        Configuration config = new Configuration().configure().addResource("hibernate.cfg.xml");
        sessionFactory = config.buildSessionFactory();
    }
    
    @Override
    public List<Project> queryProjects() {
        session = sessionFactory.openSession();
        List<Project> list = new ArrayList<Project>();
        Transaction ts = session.beginTransaction();
        String sql = "from Project project";
        list = session.createQuery(sql).setCacheable(true).list();
        ts.commit();
        session.close();
        return list;
    }

    @Override
    public void insertProject(Project pro) {
        session = sessionFactory.openSession();
        Transaction ts = session.beginTransaction();
        session.save(pro);
        ts.commit();
        session.close();
    }

    @Override
    public void updateProjectByProName(String proName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void updateProjectByID(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Project> queryProjectsByName(String proName) {
        session = sessionFactory.openSession();
        List<Project> list = new ArrayList<Project>();
        Transaction ts = session.beginTransaction();
        String sql = "from Project project where F_PRO_NAME = '"+proName+"'";
        list = session.createQuery(sql).setCacheable(true).list();
        ts.commit();
        session.close();
        return list;
    }
    
}
